package com.woniuxy.order.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.Result;
import com.woniuxy.order.config.PageInfo;
import com.woniuxy.order.dto.OrderListDto;
import com.woniuxy.order.dto.UserOrderDto;
import com.woniuxy.order.form.OrderListForm;
import com.woniuxy.order.form.UserOrderForm;
import com.woniuxy.order.param.OrderListParam;
import com.woniuxy.order.param.UserOrderParam;
import com.woniuxy.order.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>
 *  订单表 前端控制器
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@RequestMapping("/orderInfo")
@Api(tags = "订单操作接口")
public class OrderInfoController {

    @Resource
    private OrderInfoService orderInfoService;

    /**
     * 查询所有订单信息
     * @param form
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("查询订单")
    @ApiImplicitParam(name = "form",value = "搜索条件")
    public Result list(@RequestBody OrderListForm form)throws Exception{
        PageInfo<OrderListDto> onlineOrderDtoPageInfo =  orderInfoService.orderList(BeanUtil.toBean(form, OrderListParam.class));
        return Result.success("查询所有订单成功",onlineOrderDtoPageInfo);
    }

    /**
     * 下单操作
     * @param form
     * @return
     */
    @PostMapping("/userProductOrder")
    @ApiOperation("下单操作")
    @ApiImplicitParams({@ApiImplicitParam(name = "form",value = "商品"),
            @ApiImplicitParam(name = "token",value = "用户ID")
    })
    public Result userOrder(@RequestBody UserOrderForm form, @RequestHeader("token") String token)throws Exception{
        Integer accountId = (Integer) JwtUtil.getData(token, "id");
        UserOrderDto dto= orderInfoService.userOrder(BeanUtil.toBean(form, UserOrderParam.class),accountId);
        return Result.success("用户下订单成功",dto);
    }

    /**
     * 支付操作
     * @param orderNum
     * @return
     */
    @PostMapping("/payUserOrder")
    @ApiOperation("支付操作")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderNum",value = "订单编号"),
            @ApiImplicitParam(name = "token",value = "用户ID")
    })
    public Result payUserOrder(String orderNum,@RequestHeader String token) throws Exception {
        Integer accountId = (Integer) JwtUtil.getData(token, "id");
        String form=orderInfoService.payUserOrder(orderNum,accountId);
        return Result.success("用户发起支付成功",form);
    }

    /**
     * 支付回调
     * @param request
     * @return
     */
    @PostMapping("/productCallBack")
    @ApiOperation("支付回调")
    @ApiImplicitParam(name = "request",value = "支付回调")
    public String productCallBack(HttpServletRequest request) throws Exception {
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
//            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        orderInfoService.productCallBack(params);
        return "SUCCESS";
    }

    /**
     * 退款操作
     * @param orderNum
     * @return
     */
    @PostMapping("/refund")
    @ApiOperation("退款操作")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderNum",value = "订单编号"),
            @ApiImplicitParam(name = "token",value = "用户ID")
    })
    public Result refund(Integer orderNum,@RequestHeader String token) throws Exception{
        return Result.success("退款成功",null);
    }


}

